package gatewinapp;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author Thach
 */
public class FormatText {
    private String[] array_text;
    private int[] position;
    private int word_num = 8;
    
    public FormatText(String text)
    {
            this.array_text = text.split(" |\n|\r\n|\r");
            this.position = new int[array_text.length];
    }
    
    public int countFrequency(String keyword)
    {
        int count = 0;
        for(int i = 0; i < this.array_text.length ; i++){
            if(keyword.toLowerCase().equals(array_text[i].toLowerCase()) || array_text[i].toLowerCase().contains(keyword.toLowerCase())){
                this.position[count] = i;
                count++;
            }
        }
        return count;
    }
    
    public String[] leftContent(String keyword)
    {
        int number = 0;
        String str = "";
        String[] leftcontent = new String[countFrequency(keyword)];
        for(int i = 0; i < leftcontent.length; i++){
            for(int j = this.position[i]-1; j >= 0 ; j--){
                if(number < word_num){
                    str =  this.array_text[j].concat(" " + str);
                    number++;
                }
            }
            leftcontent[i] = str;
            number = 0;
            str = "";
        }
            
        return leftcontent;
    }
    
    public String[] actualText(String keyword)
    {
        String[] actualtext = new String[countFrequency(keyword)];
        for(int i = 0; i < actualtext.length; i++) {
            int j = this.position[i];
            actualtext[i] = this.array_text[j];
        }
        return actualtext;
    }
    
    public String[] rightContent(String keyword)
    {
        int number = 0;
        String str = "";
        String[] rightcontent = new String[countFrequency(keyword)];
         for(int i = 0; i<rightcontent.length; i++){
            for(int j = this.position[i]+1; j >= this.position[i] && number < word_num && j < this.array_text.length; j++){
                                   str =  str.concat(array_text[j] + " ");
                                    number++;
            }
            rightcontent[i] = str;
            number = 0;
            str = "";
         }
            return rightcontent;
    }
}
